package com.poisonh.unifiedauth.ui;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.ui.svprogresshud.SVProgressHUD;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

import java.io.File;

public class PlaybackActivity extends BaseActivity implements View.OnClickListener
{

    private static final String INTENT_NAME_VIDEO_PATH = "INTENT_NAME_VIDEO_PATH";
    private static final String RANDOM_CODE = "RANDOM_CODE";
    private VideoView mVvPlayback;

    private int mVideoCurPos;
    private String path;
    private String code;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_playback);
        initTitleView();
        initView();
        path = getIntent().getStringExtra(INTENT_NAME_VIDEO_PATH);
        code = getIntent().getStringExtra(RANDOM_CODE);
        if (path == null)
        {
            finish();
        }
        mVvPlayback = (VideoView) findViewById(R.id.vv_playback);
        mVvPlayback.setVideoPath(path);
        mVvPlayback.setKeepScreenOn(true);
        mVvPlayback.setMediaController(new MediaController(this));
        mVvPlayback.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
        {

            @Override
            public void onCompletion(MediaPlayer mp)
            {

            }
        });
        mVvPlayback.start();

    }

    @Override
    protected void initView()
    {
        setTitleTxt("视频预览");
        mLeftBtn.setOnClickListener(this);
        mRightBtn.setVisibility(View.GONE);
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText("保存");
        mTvRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
            case R.id.tv_right_txt:
                SVProgressHUD.showWithStatus(this, "加载中...");
                updateRecordVideo();
                break;
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        mVvPlayback.stopPlayback();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mVvPlayback.pause();
        mVideoCurPos = mVvPlayback.getCurrentPosition();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        mVvPlayback.seekTo(mVideoCurPos);
        mVvPlayback.start();
    }

    /**
     * 上传视频
     */
    private void updateRecordVideo()
    {
        File tempFile = new File(path);
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("video", tempFile);
        params.put("code", code);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.ADDVIDEORECORD, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                Log.d("LoginActivity", response);
                ToastUtils.showToast(PlaybackActivity.this, "视频上传成功");
                SVProgressHUD.dismiss(PlaybackActivity.this);
                finish();
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(PlaybackActivity.this, "视频上传失败");
                SVProgressHUD.dismiss(PlaybackActivity.this);
            }
        });
    }
}
